//
//  LcMediaCenter.m
//  18Birdies
//
//  Created by weihuazhang on 2016/12/1.
//  Copyright © 2016年 Lc. All rights reserved.
//
#import <AVFoundation/AVFoundation.h>

#import "LcMediaCenter.h"
#import "LcFoundation.h"


@implementation LcMediaCenter

- (BOOL)authorizationNotDetermined:(NSString *)mediaType {
  LC_CHECK_NOT_NULL(mediaType);
  AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
  return status == AVAuthorizationStatusNotDetermined;
}

- (BOOL)authorized:(NSString *)mediaType {
  LC_CHECK_NOT_NULL(mediaType);
  AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
  return status == AVAuthorizationStatusAuthorized;
}

- (BOOL)requestAuthorization:(NSString *)mediaType {
  LC_CHECK_NOT_NULL(mediaType);
  __block BOOL accessGranted = NO;
  dispatch_semaphore_t sema = dispatch_semaphore_create(0);
  [AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
    accessGranted = granted;
    dispatch_semaphore_signal(sema);
  }];
  dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
  return accessGranted;
}

@end
